package com.swak.fx.support;

import java.util.Optional;

import com.swak.fx.support.ui.UIButton;
import com.swak.fx.support.ui.UIDialog;
import com.swak.fx.support.ui.UIPanel;

import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

/**
 * 系统支持的 dialog
 * 
 * @author lifeng
 */
public class Dialogs {

	/**
	 * 错误
	 * 
	 * @param throwable
	 * @return
	 */
	public static Optional<ButtonType> error(Throwable throwable) {
		Alert alert = new Alert(AlertType.ERROR,
				"Oops! An unrecoverable error occurred.\n" + "Please contact your software vendor.\n\n"
						+ "The application will stop now.\n\n" + "Error: " + throwable.getMessage());
		return alert.showAndWait();
	}

	/**
	 * 确认
	 * 
	 * @param window
	 * @param title
	 * @param message
	 * @return
	 */
	public static ButtonType confirm(Stage window, String title, String message) {
		UIPanel panel = new UIPanel();
		panel.setHeading(new Label(title));
		panel.setBody(new Label(message));
		UIButton confirmBtn = new UIButton("确认").Success();
		UIButton cancelBtn = new UIButton("再想想").Inverse();
		panel.setActions(confirmBtn, cancelBtn);
		UIDialog dialog = new UIDialog(window);
		dialog.contentNodeProperty().set(panel);
		dialog.animationProperty().set(UIDialog.TOP_ANIMATION);
		confirmBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			dialog.setResult(ButtonType.OK);
			dialog.hide();
		});
		cancelBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			dialog.setResult(ButtonType.CANCEL);
			dialog.hide();
		});
		Optional<ButtonType> result = dialog.showAndWait();
		return result.orElse(ButtonType.CANCEL);
	}
}